<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        ul {
            width: 1200px;
            margin: auto;
            display: flex;
            flex-wrap: wrap;
        }

        li {
            width: 23%;
            margin: 1%;
            box-shadow: 0 0 1px 1px #ddd;
            text-align: center;
        }

        img {
            width: 100%;
            height: 300px;
        }
    </style>
</head>

<body>
    <div class="searchBox">
        <input type="text" class="kw">
        <button class="searchBtn">搜索</button>
    </div>
    <ul>
        <!-- <li>
            <img src="" alt="">
            <h3></h3>
            <p></p>
            <strong></strong>
        </li> -->
    </ul>


    <script src="../js/ajax.js"></script>
    <script>
        const oUl = document.querySelector('ul');

        // 页面打开的时候渲染所有的数据
        render('../php/goods_list.php');


        // 搜索
        const oKw = document.querySelector('.kw');
        const oSearch = document.querySelector('.searchBtn');

        oSearch.onclick = function () {
            // 获取关键字
            const kw = oKw.value;
            // 判断非空
            if (kw) {
                render(`../php/like.php?kw=${kw}`);
            }
        }




        function render(path) {
            ajax({
                // path:path,
                path,
                success: res => {
                    console.log(res);
                    const { status, msg, data } = res;
                    if (status) {
                        let html = '';
                        data.forEach(v => {
                            const { goods_title, goods_des, goods_price, goods_img, goods_id } = v;
                            html += `
                    <li>
                        <a href="./04detail.html?id=${goods_id}">
                            <img src="${goods_img}" alt="">
                            <h3>${goods_title}</h3>
                            <p>${goods_des}</p>
                            <strong>${goods_price}</strong>
                        </a>
                    </li>
                `
                        })
                        oUl.innerHTML = html;
                    } else {
                        alert(msg);
                    }
                }
            })
        }


    </script>

</body>

</html>